﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BitcoinCommon;
using BitcoinCommon.Data;

namespace BitcoinCommon
{
    public class MainInfo
    {
        KorbitInfo _korbitInfo;
        BTCeInfo _btceinfo;
        bool _korbitLogin = false;
        DateTime _timeLogin;


        public MainInfo()
        {
            _korbitInfo = new KorbitInfo();
            _btceinfo = new BTCeInfo();
        }

        public void Login()
        {
            if (IsLogin())
                return;


            string strURL = "";
            string strParameters = "";
            string strData = "";

            //사용자 정보를 가져온다.
            Dictionary<String, String> dicURLParameter = new Dictionary<string, string>();
            dicURLParameter.Add("client_id", _korbitInfo.ClientID);
            dicURLParameter.Add("client_secret", _korbitInfo.ClientSecret);
            dicURLParameter.Add("username", _korbitInfo.UserID);
            dicURLParameter.Add("password", _korbitInfo.UserPWD);
            dicURLParameter.Add("grant_type", "password");

            strParameters = HttpCommunication.ConvertURLParameter(dicURLParameter);
            strURL = _korbitInfo.URL + StringURL.URL_KORBIT_ACCESS_TOKEN;
            strData = RequestData(strURL, strParameters);

            //리프레시 토큰 정보를 입력한다.
            _korbitInfo.SetLogin(strData);

            RefreshTokenTime();

            _korbitLogin = true;
        }

        public bool IsLogin()
        {
            return _korbitLogin;
        }

        private void RefreshTokenTime()
        {
            _timeLogin = DateTime.Now;
        }

        public DateTime TokenTime()
        {
            return _timeLogin;
        }

        public void RefreshToken()
        {
            if ((DateTime.Now - _timeLogin).TotalMinutes < 30)
                return;

            string strURL = "";
            string strParameters = "";
            string strData = "";

            //사용자 정보를 가져온다.
            Dictionary<String, String> dicURLParameter = new Dictionary<string, string>();
            dicURLParameter.Add("client_id", _korbitInfo.ClientID);
            dicURLParameter.Add("client_secret", _korbitInfo.ClientSecret);
            dicURLParameter.Add("refresh_token", _korbitInfo.RefreshToken);
            dicURLParameter.Add("grant_type", "refresh_token");

            strParameters =  HttpCommunication.ConvertURLParameter(dicURLParameter);
            strURL = _korbitInfo.URL + StringURL.URL_KORBIT_ACCESS_TOKEN;
            strData = RequestData(strURL, strParameters);

            //리프레시 토큰 정보를 입력한다.
            _korbitInfo.SetLogin(strData);

            RefreshTokenTime();
        }

        public string RequestData(string strURL, string strParameters)
        {
            return HttpCommunication.GetRequestData(strURL, strParameters);
        }

        public string RequestData(string strURL, string strParameters, Dictionary<string, string> _dicHttpheader)
        {
            return HttpCommunication.GetRequestDataHeader(strURL, strParameters, _dicHttpheader);
        }

        public string GetKorbitURL()
        {
            return _korbitInfo.URL;
        }

        public string GetBTCeURL()
        {
            return _btceinfo.URL;
        }

        public string ConvertURLParameter(Dictionary<string, string> dicParameter)
        {
            return HttpCommunication.ConvertURLParameter(dicParameter);
        }

        public Dictionary<string, string> GetKorbitURLParameterAccessKey()
        {
            return _korbitInfo.GetURLParameterAccessKey();
        }

        public Dictionary<string, string> GetBTCURLParameterAccessKey()
        {
            return _btceinfo.GetURLParameterAccessKey();
        }

        public Dictionary<string, string> GetHttpHeaderAPI(string strURLParameter)
        {
            return HttpCommunication.GetHttpHeaderAPI(_btceinfo.ClientID, _btceinfo.ClientSecret, strURLParameter);
        }

    }
}
